<template>
  <div>
    <el-card>
      <el-button type="primary" @click="showDialog">添加角色</el-button>
      <el-table :data="roleList" border>
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column
          prop="roleName"
          label="角色名称"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column label="操作" show-overflow-tooltip>
          <template v-slot="{ row }">
            <el-button
              type="info"
              class="el-icon-info"
              @click="addPrermission(row)"
            >
              分配权限
            </el-button>
            <el-button type="danger" class="el-icon-delete">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页器 -->
      <el-pagination
        :current-page="currentPage"
        :page-sizes="[3, 6, 9, 12]"
        :page-size="pageSize"
        layout="prev, pager, next, jumper,total,sizes"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </el-card>
    <el-dialog title="添加角色" :visible.sync="dialogVisible" width="30%">
      <el-input v-model="roleName" placeholder="请输入新的用户名"></el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addRole">
          确 定
        </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Role',
  data() {
    return {
      roleList: [{}],
      currentPage: 1,
      pageSize: 3,
      total: 0,
      dialogVisible: false,
      roleName: ''
    }
  },
  mounted() {
    this.getRoleList()
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val
      this.getRoleList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getRoleList()
    },
    async getRoleList() {
      let res = await this.$API.role.reqGetRoleList(
        this.currentPage,
        this.pageSize
      )

      this.total = res.total
      this.roleList = res.items
    },
    addPrermission(row) {
      // 存到sessionStroage中
      sessionStorage.setItem(
        'row',
        JSON.stringify({
          roleName: row.roleName,
          id: row.id
        })
      )
      this.$router.push({
        name: 'RoleAuth',
        query: {
          row
        }
      })
    },
    showDialog() {
      this.dialogVisible = true
    },
    // 添加角色
    async addRole() {
      await this.$API.role.reqSaveRole(this.roleName)
      await this.getRoleList()
      this.dialogVisible = false

      this.$message.success('添加角色成功')
    }
  }
}
</script>

<style></style>
